package com.ruoyi.web.controller.his;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.his.domain.HisPurchase;
import com.ruoyi.his.domain.HisPurchase;
import com.ruoyi.his.service.IHisPurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author shebin
 * @site www.694840063@qq.com
 * @company 扬州绘鑫勤商贸有限公司
 * @create 2021-08-29 12:31
 */
@RestController
@RequestMapping("/his/purchase")
public class hisPurchaseController extends BaseController {

    @Autowired
    private IHisPurchaseService hisPurchaseService;

    /**
     * 获取单据列表
     * @param purchase
     * @return
     */
    @PreAuthorize("@ss.hasPermi('his:purchase:list')")
    @GetMapping("/list")
    public TableDataInfo list(HisPurchase purchase){
        startPage();
        List<HisPurchase> list=hisPurchaseService.selectPurchaseList(purchase);
        return getDataTable(list);
    }

    /**
     * 新增单据
     */
    @PreAuthorize("@ss.hasPermi('his:purchase:add')")
    @Log(title = "单据管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody HisPurchase purchase)
    {
        purchase.setCreateBy(getUsername());
        return toAjax(hisPurchaseService.insertPurchase(purchase));
    }

    /**
     * 删除单据
     */
    @PreAuthorize("@ss.hasPermi('his:purchase:remove')")
    @Log(title = "单据管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{purchaseId}")
    public AjaxResult remove(@PathVariable String purchaseId){
        HisPurchase purchase = hisPurchaseService.selectPurchaseById(purchaseId);
        if (purchase == null) {
            return error("项目不存在！");
        }
        if (purchase.getPurchaseStatus().equals("1")  || purchase.getPurchaseStatus().equals("2")){
            return error("审核中或入库成功不允许删除！");
        }
        return toAjax(hisPurchaseService.deletePurchaseById(purchaseId));
    }
}
